package com.dxiang.demo.web.controller;

import com.aliyun.oss.OSSClient;
import com.dxiang.demo.model.GameInfo;
import com.dxiang.demo.model.Tags;
import com.dxiang.demo.oss.AppTokenServer;
import com.dxiang.demo.service.CommonValueService;
import com.dxiang.demo.service.GameClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@Controller
public class PageController {

    @Autowired
    private AppTokenServer appTokenServer;
    @Autowired
    private CommonValueService commonValueService;
    @Autowired
    private GameClassificationService classificationService;

    /**
     *
     * @return
     */
    @RequestMapping("/index")
    public String indexHtml(){
        return "/index";
    }

    /**
     *
     * @return
     */
    @RequestMapping("/test")
    public String testHtml(Model model){
        Tags tags = new Tags();
        tags.setTagName("测试用例");
        tags.setParentId(1111111);
        model.addAttribute("helloMessage",tags);
        return "test";
    }

    /**
     *
     * @return
     */
    @PostMapping("/test")
    public String posttestHtml(@ModelAttribute Tags helloMessage){
        System.out.println(helloMessage.getParentId()+"     "+helloMessage.getTagName());
        return "400";
    }


    @RequestMapping("/saveInfo")
    public String addPage(Model model) throws Exception{
        model.addAttribute("gameInfo",new GameInfo());
        model.addAttribute("gameStatus",commonValueService.getCommonValueByParentId("3"));
        model.addAttribute("gameCompatible",commonValueService.getCommonValueByParentId("5"));
        model.addAttribute("gameLanguage",commonValueService.getCommonValueByParentId("2"));
        model.addAttribute("gameUpdate",commonValueService.getCommonValueByParentId("1"));
        model.addAttribute("gameTypes",commonValueService.getCommonValueByParentId("4"));
        model.addAttribute("gameClassification",classificationService.getGameClassification());
        return "forms";
    }

    /**
     *
     * @return
     */
    @PostMapping("/saveInfo")
    public String saveInfo(@ModelAttribute GameInfo gameInfo,
                           @RequestParam("gameStates") String gameState,
                           @RequestParam("gameUpdates")String gameUpdate,
                           @RequestParam("compatibles")String compatible,
                           @RequestParam("languages")String languages,
                           @RequestParam("gameTypes")String gameTypes,
                           @RequestParam("classifications")String classifications,
                           @RequestParam("apkFile")MultipartFile apkFile,
                           @RequestParam("apkIco")MultipartFile apkIco,
                           @RequestParam("bigImgs")List<MultipartFile> bigImgs,
                           @RequestParam("selectUis")String selectUi , Model model) throws Exception{
//        int result = gameInfoService.insert(gameInfo);
        GameInfo info = gameInfo;
        System.out.println("gameState : "+ gameState);
        System.out.println("gameUpdate : "+ gameUpdate);
        System.out.println("selectUi : "+ selectUi);
        info.getBannerImgUrl();
        appTokenServer.doUploadFile(apkFile.getOriginalFilename(),apkFile.getBytes());
        return "forms";
    }
}
